package com.central.apps.handler.contract;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.central.app.service.HandlerService;
import com.central.apps.common.util.DateUtiles;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ConClostHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private ConchangeHandler conchangeHandler;
    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {

        //合同关闭
        if (flowNode.getId().equals("appval")) {
            boolean flag = op.isFlag();//判断是否审核通过
            if (flag) {
                //查询接收入库主表gq_concollandpay
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_conclose where id=" + id);
                if (CollectionUtils.isNotEmpty(list)) {
                    //将上传的文件，添加到合同台账里边
                    String concode = (String) list.get(0).get("concode");
                    //fileSynUtilService.insertFiles(app, id, concode);
                    //将合同状态更改为关闭
                    String closttime = DateUtiles.getYmdhms(new Date());
                    String sql = "update gq_conaccount set constatus ='合同关闭' , closttime = '" + closttime + "' where concode = '" + concode + "'";
                    //String sql2 = "update gq_coninfo set constatus ='合同关闭'  where concode = '" + concode + "'";
                    handlerService.updateBySql(sql);
                    try {
                        conchangeHandler.updateStatus(concode, "合同关闭");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //handlerService.updateBySql(sql2);
                }
            }
        }
        return super.beforeExcuteFlow(app, id, flowNode, op);
    }


}
